<html>
<head>
<script type="text/JavaScript" src="scripts/services.js"></script>
<script>

function init() {
	var version = "1.6.4.2";
	
	// set icon
	var icon = localStorage["icon"];
	if(icon == undefined){
		icon = "icon.png";
		localStorage["icon"] = icon;
	}
	
	// set size
	var size = localStorage["icon-size"];
	if(size == undefined){
		size = "16";
		localStorage["icon-size"] = size;
	} else if (size == "24" || size == "42" || size == "48") {
		size = "32";
		localStorage["icon-size"] = size;
	}
	chrome.browserAction.setIcon({"path":localStorage["icon"]});
	// things to add: Reader - Subscribe,
	var myCompleteList = "";
	for (var i=0; i<globalServicesList.length; i++) {
		myCompleteList = myCompleteList + globalServicesList[i].name + ",";
	}
	localStorage.setItem('completelist', myCompleteList);
	if (localStorage.userList == undefined) {
		localStorage.setItem('userList', "Calendar,Drive,Mail,Maps,Play - Store,Plus,Reader,Settings,Web Search,YouTube,");
	} else if ((localStorage.userList != undefined) && (localStorage.version != undefined)){
		// a maybe older version is installed
		// do we have to rename or remove some obsolete services?
		intInstalledVersion = localStorage.version.replace(/\./g, "");
		if (intInstalledVersion <= 1614) {
			deleteService("Dictionary");
			deleteService("Buzz");
			deleteService("Buzz - GBuzz this");
			deleteService("Reader - GRead this");
		}
		if (intInstalledVersion <= 1615) {
			// removed
			deleteService("Aardvark");
			deleteService("Profiles");
			deleteService("US Government Search");
			deleteService("Microsoft Search");
			deleteService("BSD Unix Search");
			deleteService("Apple Macintosh Search");
			deleteService("Linux Search");
			deleteService("City Tours");
			deleteService("Code Search");
			deleteService("Health");
			deleteService("PowerMeter");
			deleteService("Page Creator");
			deleteService("Search-based keyword tool");
			deleteService("Sets");
			deleteService("Scribe");
			deleteService("News Timeline");
			deleteService("Fast Flip");
			deleteService("GOOG-411");
			deleteService("In Quotes");
			deleteService("Labs");
			deleteService("Co-op");
			deleteService("Base");
			// renamed
			renameService("Chrome extensions - Developer Dashboard", "Chrome Webstore - Developer Dashboard");
			renameService("Font Directory", "Web Fonts");
			renameService("Places", "Places - New Place");
			renameService("Hotpot", "Places");
			renameService("Shared Spaces", "Plus - Hangouts");
		}
		if (intInstalledVersion <= 1632) {
			// remove urls and tooltips from obsolete service of version 1.6.3.2
			// renamed
			renameService("Android Market", "Play - Store");
			renameService(",Music", ",Play - Music");
			renameService("Android Market Developer Console", "Android Developer Console");
			// removed
			deleteService("Realtime");
		}
		if (intInstalledVersion <= 1634) {
			// remove urls and tooltips from obsolete service of version 1.6.3.4
			// renamed
			renameService("Phone Gallery", "Android Device Gallery");
			renameService("Docs", "Drive");
			renameService("Preferences", "Search Settings");
			// removed
			deleteService("Body Browser");
			deleteService("Friend Connect");
			deleteService("Language Tools");
			deleteService("Knol");
		}
		if (intInstalledVersion <= 1635) {
			// remove urls and tooltips from obsolete service of version 1.6.3.5
			// renamed
			renameService("Insights", "Insights for Search");
		}
		if (intInstalledVersion <= 1640) {
			// remove urls and tooltips from obsolete service of version 1.6.4.0
			// renamed
			renameService("Accounts", "Settings");
			renameService("New", "About");
		}
		if (intInstalledVersion <= 1641) {
			// remove urls and tooltips from obsolete service of version 1.6.4.1
			// renamed
			renameService("Hot Trends", "Trends - Hot Searches");
			renameService(",External Keyword Tool", ",AdWords - External Keyword Tool");
			renameService(",Traffic Estimator", ",AdWords - Traffic Estimator");
			// removed
			deleteService("Wave");
		}
	}
	// set showUpdated
	if (localStorage.showUpdated == undefined) {
		localStorage.setItem("showUpdated", 'true');
	}
	// set customcount
	if (localStorage.customCount == undefined)
		localStorage.setItem("customCount", "0");
	// build wholeshebang as a difference between userlist and completelist
	var wholeshebang = "," + localStorage.completelist;
	var userlist = localStorage.userList || "";
	var userListArray = new Array();
	userListArray = userlist.split(',');
	userListArray.pop();
	var userListArrayLength = userListArray.length;
	
	wholeshebang = "," + wholeshebang;
	for (var i = 0; i < userListArrayLength; i++) {
		wholeshebang = wholeshebang.replace(',' + userListArray[i] + ',', ',');
	}
	wholeshebang = wholeshebang.substring(1);
	// save wholeshebang
	localStorage.setItem('wholeshebang', wholeshebang.substring(1));
	
	if (localStorage.showSettings == undefined) {
		localStorage.setItem('showSettings', "true");
	}
	if (localStorage.labelUnder == undefined) {
		localStorage.setItem('labelUnder', "true");
	}
	//if (localStorage.version != version) {
	// open ads on new installation
	if (localStorage.version == undefined) {
		localStorage.setItem('version', version);
		chrome.tabs.create({"url": "http://www.googleshortcuts.org/update/update_chrome.html?version=" + version + "&firstInstall=1"}, function(tab) {});
		// push in the details for the services
		updateServiceDetails();
	// open ads on new major version update: 1.5.x -> 1.5.y
	} else if ((localStorage.version.length > 4) && (localStorage.version.substr(4,1) != version.substr(4,1))) {
		localStorage.setItem('version', version);
		chrome.tabs.create({"url": "http://www.googleshortcuts.org/update/update_chrome.html?version=" + version}, function(tab) {});
		// push in the details for the services
		updateServiceDetails();
	// set update icon if minor version update
	} /*else if ((localStorage.showUpdated == "true") && (localStorage.version.length >= 5) && (localStorage.version.substr(4,1) != version.substr(4,1))) {
		localStorage.setItem('showUpdatedBadge', "true");
		chrome.browserAction.setBadgeBackgroundColor({color:[255, 204, 0, 255]});
		chrome.browserAction.setBadgeText({text:"+"});
	// in all other cases just renew everything, no ads
	}*/ else if (localStorage.version != version) {
		localStorage.setItem('version', version);
		// push in the details for the services
		updateServiceDetails();
	} else {
		// push in the details for the services in any case
		updateServiceDetails();
	}
}

function updateServiceDetails() {
	for (var i=0; i<globalServicesList.length; i++) {
		localStorage.setItem(globalServicesList[i].name + "_url", globalServicesList[i].url);
		localStorage.setItem(globalServicesList[i].name + "_tooltip", globalServicesList[i].tooltip);
	}
	console.log("updated all services from globalServicesList");
}

function renameService(serviceNameOld, serviceNameNew) {
	localStorage.userList = localStorage.userList.replace(serviceNameOld+",", serviceNameNew+",");
	localStorage.removeItem(serviceNameOld+"_url");localStorage.removeItem(serviceNameOld+"_tooltip");
}

function deleteService(serviceName) {
	localStorage.userList = localStorage.userList.replace(serviceName+",", "");
	localStorage.removeItem(serviceName+"_url");localStorage.removeItem(serviceName+"_tooltip");
}

</script>
</head>
<body onLoad="init()">
<img id="logo" src="icon.png">
</body>
</html>